package com.xywy.common.manager;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Set;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.BaseFragment;
import com.xywy.common.constants.GloableParams;
import com.xywy.common.util.MLog;
import com.xywy.find.FindFragment;
import com.xywy.fwc.FwcFragment;
import com.xywy.me.MeFragment;
import com.xywy.wjk.WjkFragment;

/**
 * 中间页面管理类，管理Fragment切換 Created by xgd on 2014/6/7.
 * 
 * @author xgd
 */
public class UiManager extends Observable {
	private static String TAG = "UiManager";

	private UiManager() {
	}

	public static UiManager getInstance() {
		return SingletonHolder.instance;
	}

	public static class SingletonHolder {
		private static UiManager instance = new UiManager();
	}

	// 记录当前Fragmengt
	private BaseFragment currentFragment;
	// 目标Fragmengt
	private BaseFragment targetFragment;
	// activity
	private BaseActivity mActivity;

	/**
	 * @return 当前显示Fragmengt
	 */
	public BaseFragment getCurrentFragment() {
		return currentFragment;
	}

	public void changeFragment(Class<? extends BaseFragment> targetClazz,
			FragmentManager fragmentManager, Bundle bundle) {

		this.mActivity = GloableParams.ACTIVITY;

		FragmentTransaction tran = fragmentManager.beginTransaction();

		// 目标Fragment唯一标识
		String target = targetClazz.getSimpleName();

		MLog.e(TAG, "目标：" + target);

		if (FragmentFactory.CACHE_MAP.containsKey(target)) {
//
			targetFragment = FragmentFactory.CACHE_MAP.get(target);
			MLog.e(TAG, "缓存中FG——" + targetFragment.getID());
		} else {
			if ("WjkFragment".equals(target)) {
				targetFragment = new WjkFragment();
//				tran.add(R.id.contentview, targetFragment);
				MLog.e(TAG, "创建了新的FG——wjk");
			}
			// if ("JtysFragment".equals(target)) {
			// targetFragment = new JtysFragment();
			// tran.add(R.id.contentview, targetFragment);
			// MLog.e(TAG, "创建了新的FG——jtys");
			// }
			if ("FindFragment".equals(target)) {
				targetFragment = new FindFragment();
//				tran.add(R.id.contentview, targetFragment);
				MLog.e(TAG, "创建了新的FG——find");
			}
			if ("MeFragment".equals(target)) {
				targetFragment = new MeFragment();
//				tran.add(R.id.contentview, targetFragment);
				MLog.e(TAG, "创建了新的FG——me");
			}
			if ("FwcFragment".equals(target)) {
				targetFragment = new FwcFragment();
//				tran.add(R.id.contentview, targetFragment);
				MLog.e(TAG, "创建了新的FG——fwc");
			}
			FragmentFactory.CACHE_MAP.put(target, targetFragment);
		}

		if(!targetFragment.isAdded())
//		 fragmentManager.beginTransaction().replace(R.id.contentview,
//		 targetFragment).commit();
		// 更新界面数据+注册耗费资源操作
		// targetFragment.onResume();
		currentFragment = targetFragment;

		// 完成底部导航切换
		changeBottomBar();

	}

	private void changeBottomBar() {
		setChanged();
		notifyObservers(currentFragment.getID());
	}

}
